using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.Common;
using T.FAS.Runtime.DataAccess.Engine;
using T.FAS.Runtime.DataAccess.SPI;
using T.FAS.Runtime.DataAccess.SQLDom;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.Error;

namespace T.FAS.Runtime.DataAccess.Service
{
    /// <summary>
    /// 统一数据访问服务变更日志服务
    /// </summary>
    public class ChangeSetService
    {

        public static ChangeSetService Instance { get; }

        static ChangeSetService()
        {
            Instance = new ChangeSetService();
        }

        public ChangeSetService()
        {

        }

        /// <summary>
        /// 获取变更日志
        /// </summary>
        /// <param name="compositeDataObject">CDO元数据实体</param>
        /// <param name="changeSetType">变更类型</param>
        /// <param name="newRecord">前端传入的newTRecord</param>
        /// <param name="pk">主键ID，删除类型时传入</param>
        /// <param name="exrParams">暂时没用</param>
        /// <returns></returns>
        public TRecord GetChangeSet(CompositeDataObject compositeDataObject, ChangeSetType changeSetType, TRecord newRecord = null, string pk = null, Dictionary<string, object> exrParams = null)
        {
            if (compositeDataObject == null)
                throw new FasException(DataAccessErrorCode.ChangeSet.Error, string.Format(CommonStringClassService.CDOMetaData_CDOInstanceCanotBeNull));
            try
            {
                CDOChangeSetService changeSetService = DataAccessEngine.GetInstance().CreateChangeSetService();
                var record = changeSetService.GetChangeSet(compositeDataObject, changeSetType, newRecord, pk, exrParams);
                return record;
            }
            catch (Exception e)
            {
                throw new FasException(DataAccessErrorCode.ChangeSet.Error, string.Format(CommonStringClassService.ChangeSetService_GetChangeSet1,compositeDataObject?.Code), e);
            }
        }

        /// <summary>
        /// 获取FireEvent变更日志，
        /// </summary>
        /// <param name="compositeDataObject"></param>
        /// <param name="newRecord"></param>
        /// <param name="ColKey"></param>
        /// <param name="EventKey"></param>
        /// <returns></returns>
        public TRecord GetFireEventChangeSet(CompositeDataObject compositeDataObject, TRecord newRecord, string ColKey, string EventKey)
        {
            if (compositeDataObject == null)
                throw new FasException(DataAccessErrorCode.ChangeSet.Error, string.Format(CommonStringClassService.CDOMetaData_CDOInstanceCanotBeNull));
            try
            {
                CDOChangeSetService changeSetService = DataAccessEngine.GetInstance().CreateChangeSetService();
                var record = changeSetService.GetFireEventChangeSet(compositeDataObject, newRecord, ColKey, EventKey);
                return record;
            }
            catch (Exception e)
            {
                throw new FasException(DataAccessErrorCode.ChangeSet.Error, string.Format(CommonStringClassService.ChangeSetService_GetFireEventChangeSet1,compositeDataObject.Code), e);
            }
        }

        public TRecord RefreshTRecord(CompositeDataObject cdo, TRecord record)
        {
            return TRecordService.RefreshTRecord(cdo, record);
        }
    }
}
